package kines.ct;

import kines.SerializableDB;
import kines.*;
import java.sql.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class MassMediaEsp extends Catalog implements SerializableDB {
  /**
   * MassMedia Id Parent
   */
  private int massMediaId;
  /**
   * Default Constructor
   */
  public MassMediaEsp( ){

  }
  /**
   * Contructor object with MassMediaEsp's id
   */
  private MassMediaEsp( int id )throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_mass_media = ConnRs.createStatement( );
    String str_mass_media = "select * from canal_esp where id_canal_esp="+id;
    ResultSet rs_mass_media = stmt_mass_media.executeQuery( str_mass_media );
    if( rs_mass_media.next() ){
      this.id = id;
      massMediaId = rs_mass_media.getInt("id_canal");
      description = rs_mass_media.getString("canal_esp");
    }
  }
  /**
   * Gets Mass Media Especial Id
   * @param massMediaId mass media id
   * @param massMediaEsp mas media Especial name
   * @return mass media especial id
   * @throws java.lang.Exception
   */
  public static int getId( int massMediaId, String massMediaEsp )throws Exception{
    int idR;
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_massMediaEsp = ConnRs.createStatement( );
    String str_massMediaEsp = "select id_canal_esp from canal_esp where id_canal="+massMediaId+" and canal_esp='"+(massMediaEsp!=null?massMediaEsp.trim():"")+"'";
    ResultSet rs_massMediaEsp = stmt_massMediaEsp.executeQuery( str_massMediaEsp );
    if( rs_massMediaEsp.next() ){
      idR = rs_massMediaEsp.getInt("id_canal_esp");
    }else{
      idR = -1;
    }
    ConnRs.close();
    return idR;
  }
  /**
   * Gets mas media id attribute
   * @return A int ( massMediaId attribute )
   */
  public int getMassMediaId( ){
    return massMediaId;
  }
  /**
   * Sets mass media id attribute
   * @param massMediaId attribute
   */
  public void setMassMediaId( int massMediaId ){
    this.massMediaId = massMediaId;
  }
  public void saveData( HttpSession session ) throws Exception {
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_insert_mass_media_esp = ConnRs.createStatement( );
    String str_insert_mass_media_esp = "insert into canal_esp (canal_esp,id_canal) values ('"+description+"',"+massMediaId+")";
    stmt_insert_mass_media_esp.execute(str_insert_mass_media_esp);
  }
  public boolean deleteData( ) throws Exception{
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method saveData() not yet implemented.");
  }
  public static Object[] getData(String where, String order) throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_mass_media = ConnRs.createStatement( );
    String str_mass_media = "select id_canal_esp from canal_esp";
    if( where != null && !where.equals("") ){
      str_mass_media +=" where "+where;
    }
    if( order != null && !order.equals("") ){
      str_mass_media +=" order by "+order;
    }
    ResultSet rs_mass_media = stmt_mass_media.executeQuery( str_mass_media );
    ArrayList arr_massMedia = new ArrayList();
    while( rs_mass_media.next() ){
      MassMediaEsp mme = new MassMediaEsp( rs_mass_media.getInt( "id_canal_esp" ) );
      arr_massMedia.add( mme );
    }
    ConnRs.close();
    return arr_massMedia.toArray();

  }
  public static MassMediaEsp getData(int id) throws Exception{
    MassMediaEsp mme = new MassMediaEsp();
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_mass_media = ConnRs.createStatement( );
    String str_mass_media = "select id_canal_esp,canal_esp from canal_esp where id_canal_esp="+id;
    ResultSet rs_mass_media = stmt_mass_media.executeQuery( str_mass_media );
    if( rs_mass_media.next() ){
      mme.setId(rs_mass_media.getInt("id_canal_esp"));
      mme.setDescription(rs_mass_media.getString("canal_esp"));
    }
    ConnRs.close();
    return mme;
  }
}
